package com.sj.purchase.modules.purchase.web;

import com.sj.purchase.common.utils.FileOperateUtil;
import com.sj.purchase.modules.purchase.repository.StockPriceRepository;
import com.sj.purchase.modules.cost.views.VshSales;
import com.sj.purchase.util.excel.ExcelUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 宋开宗 on 2017/6/24 0024.
 */
@RestController
@RequestMapping("api/stock")
public class StockController {
	@Autowired
	private StockPriceRepository repository;

	/**
	 * 上海仓销量统计
	 *
	 * @param day
	 * @param pageable
	 * @return
	 */
	@GetMapping("/shSales")
	public ResponseEntity getShSales(Date day,
									 Pageable pageable) {
		Page<VshSales> pages = repository.findShSales(day, pageable);
		Map<String, Object> map = new HashMap<>();
		map.put("shSales", pages.getContent());
		map.put("totalPages", pages.getTotalPages());
		return ResponseEntity.ok(map);
	}

	/**
	 * 上海仓销量统计Excel导出
	 *
	 * @param day
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@GetMapping("/shSales/excel")
	public void exportShSalesExcel(Date day, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Page<VshSales> pages = repository.findShSales(day, null);
		List<VshSales> shSales = pages.getContent();
		FileOperateUtil.download(request, response, DateFormatUtils.format(day, "yyyy-MM-dd") + "上海仓销量统计.xls", new ExcelUtil<VshSales>(VshSales.class).exportExcel(shSales, "上海仓销量统计"));
	}
}
